package com.yssoft.controller.app.production;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yssoft.entity.production.TWorksheet;
import com.yssoft.service.production.WorkSheetService;
import com.yssoft.utils.AppUtil;

/**
 * @author 戈超
 * @time 2018/5/11 上午 9:36
 * @生产加工单控制器
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping(value = "/app/productionSheet")
public class WorkSheetController {
	private static Logger logger=Logger.getLogger(WorkSheetService.class);
	
	@Resource
	private WorkSheetService workSheetService;
	
	/**
	 * 字典明细表一级查询
	 * @author 戈超
	 * @param request
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryDdDetailOneList", method = RequestMethod.POST)
	public Object queryDdDetailOneList(String classify, HttpServletRequest request){
		String msg = "列表查询";
    	try{
    		List<Map<String,Object>> pageList = workSheetService.queryDdDetailOneList(classify, request);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 创建加工单
	 * @author 戈超
	 * @param tWorksheet
	 * @param materialId
	 * @param materialType
	 * @param quantity
	 * @param traceability_type
	 * @param request
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "sheetSave", method = RequestMethod.POST)
	public Object sheetSave(TWorksheet tWorksheet, Integer[] materialId, String[] materialType, String[] quantity,
			String traceability_type, HttpServletRequest request){
		String msg = "生产加工单新增";
    	try{
    		int result = workSheetService.sheetSave(tWorksheet, materialId, materialType, quantity, traceability_type, request);
        	if(result > 0){
        		return AppUtil.returnMapJson("1", msg+"成功!", null);
        	} else {
        		return AppUtil.returnMapJson("0", msg+"失败!", null);
        	}
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产加工单修改回显
	 * @author 戈超
	 * @param wsId
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "querySheetById", method = RequestMethod.POST)
	public Object querySheetById(Integer wsId){
		String msg = "生产加工单修改回显";
    	try{
    		Map<String, Object> result = workSheetService.querySheetById(wsId);
        	return AppUtil.returnMapJson("1", msg+"成功!", result);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产加工单编辑
	 * @author 戈超
	 * @param tWorksheet
	 * @param materialId
	 * @param materialType
	 * @param quantity
	 * @param traceability_type
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "sheetEdit", method = RequestMethod.POST)
	public Object sheetEdit(TWorksheet tWorksheet, Integer[] materialId, String[] materialType, 
			String[] quantity, String traceability_type){
		String msg = "生产加工单修改";
    	try{
    		int result = workSheetService.sheetEdit(tWorksheet, materialId, materialType, quantity, traceability_type);
        	if(result > 0){
        		return AppUtil.returnMapJson("1", msg+"成功!", null);
        	} else {
        		return AppUtil.returnMapJson("0", msg+"失败!", null);
        	}
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产加工单删除
	 * @author 戈超
	 * @param id
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "sheetDelete", method = RequestMethod.POST)
	public Object sheetDelete(Integer id){
		String msg = "生产加工单删除";
    	try{
    		int result = workSheetService.sheetDelete(id);
    		if(result > 0){
    			return AppUtil.returnMapJson("1", msg+"成功!", null);
    		} else {
    			return AppUtil.returnMapJson("0", msg+"失败!", null);
    		}
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产加工单确认完成
	 * @author 戈超
	 * @param id
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "complete", method = RequestMethod.POST)
	public Object complete(Integer id){
		String msg = "确认完成";
    	try{
    		int result = workSheetService.complete(id);
    		if(result > 0){
    			return AppUtil.returnMapJson("1", msg+"成功!", null);
    		} else {
    			return AppUtil.returnMapJson("0", msg+"失败!", null);
    		}
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
}
